package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 山顶洞人郑某
 * @className ShoppingCartServiceImpl.java
 * @date 2024 02 08
 */
@Slf4j

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService{
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 查看购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        Long currentUserId = BaseContext.getCurrentUserId();
        return shoppingCartMapper.listByuserId(currentUserId);
    }

    /**
     * 添加购物车信息
     *
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentUserId());
        log.info("当前用户id为====={}=====",BaseContext.getCurrentUserId());
        //判断当前添加的菜品/套餐是否在购物车
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if (list!=null&&list.size()>0){
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber()+1);
            shoppingCartMapper.updateCountById(cart);
        }else {
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null){
                Dish dish = dishMapper.getById(dishId);
                log.info("查出的菜品信息为{}====：",dish);
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setDishId(shoppingCartDTO.getDishId());

            }
            else {
                Long setmealId = shoppingCartDTO.getSetmealId();
                Setmeal setmeal = setmealMapper.getById(setmealId);
                log.info("查出的套餐信息为{}===",setmeal);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setSetmealId(shoppingCartDTO.getSetmealId());

            }
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 剪掉购物车中的一个商品
     *
     * @param shoppingCartDTO
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentUserId());
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if (list!=null && list.size()>0){
            ShoppingCart cart = list.get(0);
            shoppingCart.setNumber(cart.getNumber()-1);
            if (shoppingCart.getNumber()<=0){
                shoppingCartMapper.deleteById(cart.getId());
                return;
            }
        }
        shoppingCartMapper.sub(shoppingCart);
    }

    /**
     * 清空购物车
     */
    @Override
    @Transactional
    public void clean() {
        Long userId = BaseContext.getCurrentUserId();

        shoppingCartMapper.clean(userId);
    }

}
